package com.lh.cookie与session的研究;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * 作者：idea
 * 日期：2018/7/2
 * 描述：
 */
@WebServlet(name = "/cookie",urlPatterns = "/cookie")
public class CookieHandleServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookies[]=req.getCookies();
        Enumeration<String> headerNames=req.getHeaderNames();
        PrintWriter out=resp.getWriter();
        while(headerNames.hasMoreElements()){
            String name=headerNames.nextElement();
            out.write(name+"--->"+req.getHeader(name)+"\n");
        }
        out.write("--------------------\n");

        for (Cookie cookie : cookies) {
            out.write(cookie.getName()+" : "+cookie.getValue()+"\n");
        }
        out.close();
        Cookie cookie=new Cookie("response","this is a test cookie");
        resp.addCookie(cookie);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("this is destroy");
    }

    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("this is init");
    }
}
